/**
 * 
 */
package com.xb.demo22.command;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
 * 命令 、 事件驱动
 * 
 * @author kexue
 *
 */
public class TestMain extends JFrame implements ActionListener, MouseMotionListener, WindowListener {

	private CommandStack history = new CommandStack();

	private JButton jButton = new JButton("清除");
	private JButton undoBtn = new JButton("撤销");

	private DrawCanvas drawCanvas = new DrawCanvas(400, 400, history);

	public TestMain(String[] args) {
		super("test");
		this.addWindowListener(this);
		drawCanvas.addMouseMotionListener(this);
		jButton.addActionListener(this);
		
		//撤销 
		undoBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				history.undoCommand();
				drawCanvas.repaint();
			}
		});

		Box box = new Box(BoxLayout.X_AXIS);
		box.add(jButton);
		box.add(undoBtn);
		Box mainbox = new Box(BoxLayout.Y_AXIS);
		mainbox.add(box);
		mainbox.add(drawCanvas);

		getContentPane().add(mainbox);
		pack();
		show();
	}

	public static void main(String[] args) {
		new TestMain(args);
	}

	public void mouseDragged(MouseEvent e) {
		DrawCommand command = new DrawCommand(drawCanvas, e.getPoint());
		history.addCommand(command);
		command.execute();
	}

	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
	}

	public void actionPerformed(ActionEvent e) {
		history.clear();
		drawCanvas.repaint();
	}

	/////////////////////////////////////

	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
	}

	public void windowClosing(WindowEvent e) {
		System.exit(0);
	}

	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
	}

	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
	}

	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
	}

	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
	}

	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
	}

}
